﻿/**********************************************************
*-----------------------------------*
* flareJ JavaScript Library
*-----------------------------------*
* 作者:Joe_Sky
* 邮箱:hitomi_doax@tom.com
* 最后更新时间:2011-9-7
*-----*-----*-----*-----*-----*-----*
* 基于jQuery
***********************************************************/

/*----------------------------------------------*
* flareJ.Datagrid
*-------*-------*-------*-------*-------*-------*
* 表格
*-------*-------*-------*-------*-------*-------*
* (必须)flareJ.Base.js
*----------------------------------------------*/
(function ($) {
    var FJ = $.flareJ, flareJ = FJ;

    /************************************************************
    *-----------------------------------------------------------*
    *                           表格
    *-----------------------------------------------------------*
    *************************************************************
    *-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*
    * 
    *-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*
    * 
    ************************************************************/
    this.DatagridJ = this.DGJ = FJ.DatagridJ = FJ.DGJ = FJ.CJ.extend({
        //#region 构造方法
        init: function (elemObj, settings) {
            //参数
            this._super(elemObj, $.extend(true, {
                fjType: "DGJ",
                renderTo: elemObj,
                renderType: "prepend",
                columns: [                             //列模板
                    {text: "ID", dataIndex: 'id' }
                ],
                emptyTxt: "没有数据",                   //没数据时显示的字
                emptyRowH: 30,                         //没数据时行高
                hasChk: false,                         //是否有复选框
                hasChklbl: false,                      //是否有全选标签
                hasChkAll: false,                      //是否有全选框
                hasChkG: false,                        //分组行是否有复选框
                hasChkGR: false,                       //每组的子行是否有复选框
                isCheckOne: false,                     //只能单选
                defaultH: 20,                          //默认行高
                defaultW: 100,                         //默认列宽
                defaultMinW: 30,                       //默认最小列宽
                defaultAlign: "center",                //默认对齐方式
                lockColumn: 0,                         //锁定列
                lockColumnR: 0,                        //锁定列(右侧)
                scrollBarH: null,                      //锁定列横向滚动条渲染元素
                groupColumn: null,                     //锁定列后将第一个表按该字段分组
                groupRow: null,                        //显示分组行
                renderGroupRow: null,                  //构建分组行
                renderSonRow: null,                    //构建分组内行
                asyncLoadSonRow: false,                //是否异步加载分组内行
                groupRowH: null,                       //分组行行高
                useSingleThColor: false,               //表头使用单色
                sbhStyle: null,                        //下滚动条样式
                chkWidth: 20,                          //全选列头宽度
                canColumeDD: true,                     //是否可拖动列宽
                canSort: true,                         //是否可排序
                isLazyLoad: false,                     //是否延时加载
                isPageCache: false,                    //分页缓存
                hasFloatBtn: true,                    //是否有右上角浮动按钮
                widthF: 450,                           //最大宽度
                heightF: "auto",                       //最大高度
                borderWidth: 0,
                colorParams: {
                    borderOut: "#d0d8e8",
                    bgColor: "#ffffff",                     //背景色
                    highlight: "#dddddd",                   //鼠标移上高亮
                    trColor1: "#ffffff",                    //奇数行颜色
                    trColor2: "#F8F8D3",                    //偶数行颜色
                    trColor3: "#F8F8D3",                    //分组显示时第一个表行颜色
                    thColor: "#FEF6E1",                     //表头颜色
                    thColor1: "#ffffff",                    //表头渐变色1
                    thColor2: "#DEF5FD",                    //表头渐变色2
                    groupRow: "#FFF7E7"
                },
                evts: {
                    dataLoaded: null,                       //数据加载完毕
                    afterSetWidth: null                     //设置宽度完毕
                },
                eType: (function (j) {
                    return {
                        afterrender: j
                    }
                })("DGJ")
            }, settings));

            this.data = [];

            this.initFn();

            return this;
        },
        //#endregion

        //#region 初始化
        initFn: function () {
            this.data = [];  //数据列表
            this.rows = [];  //行集合
            this._super();
            this.isLoading = false;

            //使用分页缓存
            if(this.p.isPageCache){
                this.pageCache = [];
                this.datas = [];
            }

            this.create();
            this.show();

            //初始化
            this.clearData(true);
        },
        //#endregion

        //#region 构建
        create: function () {
            var thiz = this;
            this._super();

            //外层
            this.divOut.attr("id", "DGJ_" + this.objId).addClass("dgj").css({
                width: this.p.widthF,
                height: this.p.heightF
            });

            //#region 构建表体
            //表头容器
            this.headContainer = $('<div class="dgj_headContainer"></div>');

            //表体容器
            this.bodyContainer = $('<div class="dgj_bodyContainer"></div>');
            this.bodyContainer.bind("scroll", function () {
                thiz.headContainer.scrollLeft($(this).scrollLeft());

                if (FJ.isIE7 && thiz.p.lockColumn) {  //解决ie7下冻结表体不随滚动条滚动问题
                    thiz.lcContainer.css("position", "static");
                    if (thiz.dgjStoScV) clearTimeout(thiz.dgjStoScV);
                    thiz.dgjStoScV = setTimeout(function () {
                        thiz.lcContainer.css("position", "relative");
                        var scrollL = thiz.sbj.getScrollLeft();
                        if (scrollL > 0) {
                            thiz.sbj.setScrollLeft(scrollL + 1);
                            thiz.sbj.setScrollLeft(scrollL - 1);
                        }
                        else {
                            thiz.setDDPosition(true);
                        }
                    }, 500);
                }
            });

            //表头
            this.tableHead = $('<table></table>');

            //主体容器(头部)
            this.mainHeadContainer = $('<div class="dgj_mainHeadContainer"></div>');
            this.mainHeadContainer.append(this.tableHead);

            //表体
            this.table = $('<table class="dgj_table1"></table>');

            //主体容器
            this.mainContainer = $('<div class="dgj_mainContainer"></div>');
            this.mainContainer.append(this.table);

            //表头
            this.tableHead2 = $('<table></table>').css("margin-left", -1);

            //锁定列表容器(头部)
            this.lcHeadContainer = $('<div class="dgj_lcHeadContainer"></div>');
            this.lcHeadContainer.append(this.tableHead2);

            //锁定列表容器
            this.lcContainer = $('<div class="dgj_lcContainer"></div>');

            //表头
            this.tableHead3 = $('<table></table>').css("margin-left", -1);

            //锁定列表容器(右,头部)
            this.rightHeadContainer = $('<div class="dgj_rightHeadContainer"></div>');
            this.rightHeadContainer.append(this.tableHead3);

            //锁定列表容器(右)
            this.rightContainer = $('<div class="dgj_rightContainer"></div>').css("margin-left", -1);

            //表格容器
            this.gridContainer = $('<div class="dgj_gridContainer"></div>');
            this.gridContainer.append(this.headContainer.append(this.mainHeadContainer)).append(this.bodyContainer.append(this.mainContainer));

            //下滚动条容器
            this.sBarHContainer = $('<div class="dgj_sBarHContainer"></div>');
            //#endregion

            //#region 浮动按钮
            if(this.p.hasFloatBtn && !this.p.lockColumn) {
                this.floatBtn = $('<input type="button" title="点击可设置隐藏列" onclick="this.focus()" hidefocus id="dgj_floatBtn_' + this.objId + '" class="dgj_floatBtn" />');

                //功能菜单
                this.ttjMenu = $(this.floatBtn).TTJ({
                    hoverDirect: "left",
                    shiftLeft: -90,
                    shiftTop: 20,
                    hoverName: "dgj_floatBtn_" + this.objId,
                    hoverType: "focus",
                    speed: 1,
                    showSpeed: 300,
                    loadSpeed: 100,
                    onlyFirstLoad: true,
                    widthF: 100,
                    heightF: 50,
                    urlType: "html",
                    fnHtml: function () {
                        var oTable = $('<table class="dgj_menu"></table>');
                        this.oTable = oTable;
                        var oTr = $('<tr></tr>');
                        var oTd = $('<td style="color:#666666;font-size: 12px;font-family: 微软雅黑;padding-left:5px;"></td>');
                        var oTbody = $('<tbody></tbody>');
                        oTable.append(oTbody);

                        var oTr1 = oTr.clone().append(oTd.clone().addClass("dgj_menu_td1").append('<div class="dgj_menu_parent"></div>')).append(oTd.clone().text("隐藏列"));
                        oTbody.append(oTr1.attr("id", "tr1_dgjMenu_" + thiz.objId));
                        //oTbody.append(oTr.clone().append(oTd.clone().addClass("dgj_menu_td1").append('<div class="dgj_menu_parent"></div>')).append(oTd.clone().text("冻结列")));

                        oTable.find("tr").hover(function(){
                            var url = FJ.imgPath + "Datagrid/favoriteIconBg_over.png";
                            var tds = $(this).find("td");
                            tds.eq(0).css("background", "url(" + url + ") no-repeat left center");
                            tds.eq(1).css("background", "url(" + url + ") no-repeat right center");
                        },function(){
                            $(this).find("td").css("background","none");
                        });

                        //隐藏列二级菜单
                        thiz.ttjMenu2 = $(oTr1).TTJ({
                            hoverDirect: "left",
                            shiftLeft: -4,
                            shiftTop: 23,
                            hoverName: "dgj_floatBtn2_" + thiz.objId,
                            speed: 1,
                            showSpeed: 300,
                            loadSpeed: 100,
                            onlyFirstLoad: true,
                            widthF: 100,
                            heightF: 50,
                            urlType: "html",
                            fnHtml: function () {
                                var oTable = $('<table class="dgj_menu"></table>');
                                this.oTable = oTable;
                                var oTr = $('<tr></tr>');
                                var oTd = $('<td style="color:#666666;font-size: 12px;font-family: 微软雅黑;padding-left:5px;"></td>');
                                var oTbody = $('<tbody></tbody>');
                                oTable.append(oTbody);

                                //生成各列菜单
                                for (var cn = 0, cols = thiz.p.columns; cn < cols.length; cn++) {
                                    (function(cn) {
                                        var oChk = $('<input type="checkbox" checked />').click(function(e){
                                            e.stopPropagation();
                                            if(!thiz.hideColumn(cn, oChk.attr("checked"))) {
                                                oChk.attr("checked", true);
                                            }
                                            else{
                                                thiz.p.columns[cn].isHide = !oChk.attr("checked");
                                            }
                                        });
                                        var oTr1 = oTr.clone().append(oTd.clone().addClass("dgj_menu_td1").append(oChk)).append(oTd.clone().text(cols[cn].text));
                                        oTr1.click(function() {
                                            oChk.attr("checked", oChk.attr("checked") ? false : true);
                                            if(!thiz.hideColumn(cn, oChk.attr("checked"))) {
                                                oChk.attr("checked", true);
                                            }
                                            else{
                                                thiz.p.columns[cn].isHide = !oChk.attr("checked");
                                            }
                                        });

                                        oTbody.append(oTr1);
                                    })(cn);
                                }

                                oTable.find("tr").hover(function(){
                                    var url = FJ.imgPath + "Datagrid/favoriteIconBg_over.png";
                                    var tds = $(this).find("td");
                                    tds.eq(0).css("background", "url(" + url + ") no-repeat left center");
                                    tds.eq(1).css("background", "url(" + url + ") no-repeat right center");
                                },function(){
                                    $(this).find("td").css("background","none");
                                });

                                return oTable;
                                return "";
                            },
                            colorParams: {
                                borderIn: "#196FA0",
                                bgColorBody: "#fefee9",
                                bgColorBodyB: "#fefee9"
                            },
                            evts: {
                                afterbodyload: function(){
                                    var h = this.oTable.height();
                                    if(h <= 300){
                                        this.bodyIn.css({
                                            overflow: "hidden"
                                        });
                                    }
                                    else {
                                        h = 300;
                                        this.bodyIn.css({
                                            overflowX: "hidden",
                                            overflowY: "auto"
                                        });
                                    }
                                    this.divOut.height(h);
                                    this.p.heightF = h;
                                },
                                onMouseenter: function(){
                                    thiz.ttjMenu.overByFocus = true;
                                },
                                onMouseleave: function(){
                                    thiz.ttjMenu.overByFocus = false;
                                    thiz.floatBtn.focus();
                                }
                            }
                        });

                        return oTable;
                        return "";
                    },
                    colorParams: {
                        borderIn: "#196FA0",
                        bgColorBody: "#fefee9",
                        bgColorBodyB: "#fefee9"
                    },
                    evts: {
                        afterbodyload: function(){
                            var h = this.oTable.height();
                            this.divOut.height(h);
                            this.bodyIn.css({
                                overflow:"hidden"
                            });
                            this.p.heightF = h;
                        }
                    }
                });

                //加载
                this.divOut.append(this.floatBtn);
            }
            //#endregion

            //#region 冻结表
            //锁定列表
            if (this.p.lockColumn) {
                this.mainHeadContainer.css("width", "auto");
                this.mainContainer.css("width", "auto");
                this.table2 = $('<table class="dgj_table2"></table>').css("margin-left", -1);
                this.lcContainer.append(this.table2);
                this.thead2 = $('<thead></thead>');
                this.trH2 = $('<tr></tr>');
                this.thead2.append(this.trH2);
                this.tbody2 = $('<tbody></tbody>');
                this.tableHead2.append(this.thead2);
                this.table2.append(this.tbody2);
            }

            //锁定列表(右)
            if (this.p.lockColumnR) {
                this.table3 = $('<table class="dgj_table3"></table>');
                this.mainContainer.css("width", "auto");
                this.rightContainer.append(this.table3);
                this.thead3 = $('<thead></thead>');
                this.trH3 = $('<tr></tr>');
                this.thead3.append(this.trH3);
                this.tbody3 = $('<tbody></tbody>');
                this.tableHead3.append(this.thead3);
                this.table3.append(this.tbody3);
            }
            //#endregion

            //#region 表头
            //表头
            this.thead = $('<thead></thead>');
            this.trH = $('<tr></tr>');
            this.thead.append(this.trH);
            this.tableHead.append(this.thead);
            if (this.p.hasChk) {  //是否有复选框
                this.chks = [];
                this.thAll = $('<th style="text-align:center;"></th>');
                this.tdDivAll = $('<div class="dgj_tdDiv"></div>').css({
                    width: this.p.chkWidth
                });
                this.thAll.append(this.tdDivAll);

                if (this.p.hasChkAll) {  //是否有全选框
                    this.chkAll = $('<input title="全选" id="dgj_chkall_' + this.p.objId + '" type="checkbox" />').click(function () {
                        thiz.fnChkAll(this);
                        thiz.fire("checkedAll", { checked: this.checked });
                    });
                    this.tdDivAll.append(this.chkAll);
                    if (this.p.hasChklbl)
                        this.tdDivAll.append('<label for="dgj_chkall_' + this.p.objId + '">全选</label>');
                }
                this.trH.append(this.thAll);
            }

            //加载表头列
            for (var i = 0, cols = this.p.columns; i < cols.length; i++) {
                var oTrH;
                if (!this.p.lockColumn) {
                    oTrH = this.trH;
                }
                else {
                    if (i < this.p.lockColumn - 1) {
                        oTrH = this.trH;
                    }
                    else {
                        if (this.p.lockColumnR) {
                            if (i >= this.p.lockColumnR - 1) {
                                oTrH = this.trH3;
                            }
                            else {
                                oTrH = this.trH2;
                            }
                        }
                        else {
                            oTrH = this.trH2;
                        }
                    }
                }

                oTrH.append((function (c, i) {
                    var thDiv = $('<div class="dgj_tdDiv"></div>').css({
                        width: thiz.p.defaultW
                    });
                    thDiv.append(c.text);
                    var sortImg = $('<img class="dgj_sortImg" />');
                    thDiv.append(sortImg.hide());

                    var th = $('<th></th>').css({
                        height: thiz.p.defaultH
                    });

                    if (!thiz.p.renderGroupRow) {
                        th.attr("title", "点击排序");
                        th.click(function () {  //点击排序
                            if(thiz.p.canSort) {
                                var src = sortImg.attr("src");
                                if (!src || src.indexOf("desc") != -1) {
                                    sortImg.attr("src", FJ.imgPath + "Datagrid/sort_asc.gif").css("display", "inline");
                                    thiz.sort(true, i, c.dataIndex);
                                }
                                else {
                                    sortImg.attr("src", FJ.imgPath + "Datagrid/sort_desc.gif");
                                    thiz.sort(false, i, c.dataIndex);
                                }
                            }
                        });
                    }

                    if (c.width) {
                        thDiv.css({
                            width: c.width
                        });
                    }
                    else {
                        thiz.columnP = i;  //记录未设置宽度的列数
                    }
                    th.append(thDiv);

                    return th;
                })(cols[i], i));
            }
            //#endregion

            //#region 加载表体
            //表体
            this.tbody = $('<tbody></tbody>');
            this.table.append(this.tbody);
            this.divOut.append(this.gridContainer).append(this.sBarHContainer);

            //横向滚动条
            this.sbj = $(this.sBarHContainer).SBJ({
                style: this.p.sbhStyle,
                widthF: 0,
                widthC: 0,
                evts: {
                    scroll: function (e, p) {
                        if (!thiz.p.lockColumn) {
                            thiz.headContainer[0].scrollLeft = p.s.scrollLeft;
                            thiz.bodyContainer[0].scrollLeft = p.s.scrollLeft;

                            if (thiz.dgjStoScroll) clearTimeout(thiz.dgjStoScroll);
                            thiz.dgjStoScroll = setTimeout(function () {
                                //计算拖动条位置
                                if (thiz.p.canColumeDD) {
                                    thiz.setDDPosition();
                                }
                            }, 100);
                        }
                        else {
                            thiz.lcHeadContainer[0].scrollLeft = p.s.scrollLeft;
                            thiz.lcContainer[0].scrollLeft = p.s.scrollLeft;

                            if (thiz.dgjStoScroll) clearTimeout(thiz.dgjStoScroll);
                            thiz.dgjStoScroll = setTimeout(function () {
                                //计算拖动条位置
                                if (thiz.p.canColumeDD) {
                                    thiz.setDDPosition(true);
                                }
                            }, 100);
                        }
                    }
                }
            });

            //加载锁定表
            if (this.p.lockColumn) {
                this.headContainer.append(this.lcHeadContainer);
                this.bodyContainer.append(this.lcContainer);

                //页面布局时控件重新布局
                $(window).bind("resize", function () {
                    if (thiz.dgjSto) clearTimeout(thiz.dgjSto);
                    thiz.dgjSto = setTimeout(function () {
                        thiz.setWidth();
                    }, 500);
                });
            }

            //加载锁定表(右)
            if (this.p.lockColumnR) {
                this.headContainer.append(this.rightHeadContainer);
                this.bodyContainer.append(this.rightContainer);
            }

            //设置表头颜色
            if (this.p.useSingleThColor) {
                this.bodyContainer.find("th").css({
                    backgroundImage: "none",
                    backgroundColor: this.p.colorParams.thColor
                });
            }

            //是否可拖动列宽
            if (this.p.canColumeDD) {
                this.setColumeDD();
            }

            //页面布局时控件重新计算主体高度
            $(window).bind("resize", function () {
                if (thiz.dgjStoMH) clearTimeout(thiz.dgjStoMH);
                thiz.dgjStoMH = setTimeout(function () {
                    if (!thiz.p.lockColumn) {
                        thiz.setTableWidth();
                    }
                    thiz.setMainHeight();
                }, 500);
            });

            //创建完毕
            this.fire("afterrender", "DGJ");
            //#endregion

            return this;
        },
        //#endregion

        //#region 加载数据
        loadData: function (data, pageNo, total) {
            var thiz = this;
            this.table.css("width", "auto");
            this.data = data;
            this.rows = [];
            this.groupObj = {};  //分组标记(区分加载数据)
            this.groupArr = [];  //分组标记(区分计算同步行高)
            if (this.p.hasChk && this.p.hasChkAll)   //初始化复选框
                this.chkAll.attr("checked", false);

            if (data.length > 0) {
                //加载行
                if(!this.p.isPageCache){
                    this.chks = [];

                    //清空表
                    this.tbody.empty();
                    if (this.p.lockColumn) {
                        this.tbody2.empty();
                    }
                    if (this.p.lockColumnR) {
                        this.tbody3.empty();
                    }
                    for (var i = 0; i < data.length; i++) {
                        this.addRow(data[i], i, null, true);
                    }
                }
                else{
                    this.pageNo = pageNo;   //记录当前页码
                    this.pageCacheTotal = total;   //页缓存数据总数

                    var cache = this.getPageCache(pageNo);  //获取分页缓存
                    var oTbody;
                    if(cache){   //该页有缓存
                        oTbody = cache[2];
                        this.chks = cache[3];

                        this.tbody.hide();
                        oTbody.show();
                        this.tbody = oTbody;

                        if (this.p.lockColumn) {
                            var oTbody2 = cache[4];
                            this.tbody2.hide();
                            oTbody2.show();
                            this.tbody2 = oTbody2;
                        }
                        if (this.p.lockColumnR) {
                            var oTbody3 = cache[5];
                            this.tbody3.hide();
                            oTbody3.show();
                            this.tbody3 = oTbody3;
                        }
                    }
                    else{   //该页无缓存
                        this.chks = [];

                        if(this.tbody){
                            if(this.pageCache.length <= 0){
                                this.tbody.remove();
                                if (this.p.lockColumn) {
                                    this.tbody2.remove();
                                }
                                if (this.p.lockColumnR) {
                                    this.tbody3.remove();
                                }
                            }
                            else{
                                this.tbody.hide();
                                if (this.p.lockColumn) {
                                    this.tbody2.hide();
                                }
                                if (this.p.lockColumnR) {
                                    this.tbody3.hide();
                                }
                            }
                        }
                        this.tbody = $('<tbody></tbody>');
                        this.table.append(this.tbody);
                        if (this.p.lockColumn) {
                            this.tbody2 = $('<tbody></tbody>');
                            this.table2.append(this.tbody2);
                        }
                        if (this.p.lockColumnR) {
                            this.tbody3 = $('<tbody></tbody>');
                            this.table3.append(this.tbody3);
                        }

                        //加入分页缓存
                        this.pageCache.push([pageNo, this.data, this.tbody, this.chks, this.p.lockColumn ? this.tbody2 : null, this.p.lockColumnR ? this.tbody3 : null]);

                        for (var i = 0; i < data.length; i++) {
                            this.addRow(data[i], i, null, true, null, null, pageNo);
                        }

                        this.joinCacheData();  //连接缓存数据集
                    }
                }

                this.setTableWidth();

                if (this.p.lockColumn) {
                    //同步行高
                    this.syncRowHeight();

                    //设置下滚动条位置
                    this.sbj.left(this.table.width());
                }

                this.setMainHeight();
                this.p.canSort = true;  //开启排序

                //隐藏列
                for (var i = 0, cols = this.p.columns; i < cols.length; i++) {
                    if(cols[i].isHide){
                        this.hideColumn(i, false, true);
                    }
                }

                this.fire("dataLoaded", { data: data });
            }
            else {
                this.clearData();
            }

            this.sbj.setScrollLeft(0);
        },
        //#endregion   

        //#region 加载分组行数据
        loadDataG: function (data, pageNo, total) {
            var thiz = this;
            this.data = data;
            this.rows = [];
            if (this.p.hasChkG && this.p.hasChkAll)   //初始化复选框
                this.chkAll.attr("checked", false);

            if (data.length > 0) {
                //加载行
                if(this.p.isLazyLoad) {  //延时加载
                    this.tbody.empty();   //清空表

                    this.iLazy1 = 0;
                    this.iLazy2 = 0;
                    for(var i = 0;i < this.data.length;i++){
                        this.iLazy1 += this.data[i].sonRows.length;
                    }

                    this.guidLazy = new Date().getTime();   //延时加载唯一标记
                    this.isLoading = true;   //正在加载中标记

                    var i = 0;
                    var fn = function(){
                        if(fn.guidLazy != thiz.guidLazy)   //如果延时加载唯一标记和本次加载不同,退出
                            return;
                        if(i>=data.length) {
                            thiz.setTableWidth();
                            thiz.setMainHeight();
                            thiz.sbj.setScrollLeft(0);
                            thiz.fire("dataLoaded", { data: data });
                            return;
                        }
                        thiz.addRowGS(data[i], i);
                        i++;
                        setTimeout(arguments.callee,0);
                    };
                    fn.guidLazy = this.guidLazy;
                    fn();
                }
                else{
                    if(!this.p.isPageCache){
                        this.chks = [];
                        this.tbody.empty();   //清空表
                        for (var i = 0; i < data.length; i++) {
                            this.addRowGS(data[i], i);
                        }
                    }
                    else{
                        this.pageNo = pageNo;   //记录当前页码
                        this.pageCacheTotal = total;   //页缓存数据总数

                        var cache = this.getPageCache(pageNo);  //获取分页缓存
                        var oTbody;
                        if(cache){   //该页有缓存
                            oTbody = cache[2];
                            this.chks = cache[3];

                            this.tbody.hide();
                            oTbody.show();
                            this.tbody = oTbody;
                        }
                        else{   //该页无缓存
                            this.chks = [];

                            if(this.tbody){
                                if(this.pageCache.length <= 0){
                                    this.tbody.remove();
                                }
                                else{
                                    this.tbody.hide();
                                }
                            }
                            this.tbody = $('<tbody></tbody>');
                            this.table.append(this.tbody);
                            
                            //加入分页缓存
                            this.pageCache.push([pageNo, this.data, this.tbody, this.chks]);

                            for (var i = 0; i < data.length; i++) {
                                this.addRowGS(data[i], i, pageNo);
                            }

                            this.joinCacheData();  //连接缓存数据集
                        }
                    }

                    this.setTableWidth();
                    this.setMainHeight();
                    this.sbj.setScrollLeft(0);
                    this.fire("dataLoaded", { data: data });
                }

                //隐藏列
                for (var i = 0, cols = this.p.columns; i < cols.length; i++) {
                    if(cols[i].isHide){
                        this.hideColumn(i, false, true);
                    }
                }
            }
            else {
                this.clearData();
            }
        },
        //#endregion

        //#region 按页码获取分页缓存数据
        getPageCache: function (pageNo) {
            var cache = false;
            if(this.p.isPageCache) {
                for (var i = 0; i < this.pageCache.length; i++) {
                    if(this.pageCache[i][0] == pageNo){
                        cache = this.pageCache[i];
                        break;
                    }
                }
            }
            return cache;
        },
        //#endregion

        //#region 清空页缓存
        clearPageCache: function () {
            if(this.p.isPageCache){
                this.pageCache = [];
                this.datas = [];
                this.table.find("tbody:hidden").remove();
            }
        },
        //#endregion

        //#region 将分页缓存各页数据集拼接为一个数组
        joinCacheData: function () {
            this.datas = [];
            for (var i = 0; i < this.pageCache.length; i++) {
                $.merge(this.datas, this.pageCache[i][1]);
            }
        },
        //#endregion

        //#region 隐藏列
        hideColumn: function (colNo, isHide, isLoaded) {
            //保持至少有一列显示
            if(!isHide && !isLoaded && this.headContainer.find("th:hidden").length >= this.p.columns.length - 1) {
                return false;
            }

            var no = colNo;
            if(this.p.hasChk){
                no += 1;
            }
            var hideOrShow = "hide";
            if(isHide){
                hideOrShow = "show";
            }

            var colType = 1;
            if (!this.p.lockColumn) {   //无冻结列
                colType = 0;
            }
            else {   //有冻结列
                if (colNo < this.p.lockColumn - 1) {  //冻结列(左)
                    colType = 1;
                }
                else {
                    if (this.p.lockColumnR) {  //右侧有冻结列
                        if (colNo >= this.p.lockColumnR - 1) {  //冻结列(右)
                            colType = 3;
                        }
                        else {
                            colType = 2;
                        }
                    }
                    else {
                        colType = 2;
                    }
                }
            }

            switch (colType) {
                case 0: {
                        this.tableHead.find("th").eq(no)[hideOrShow]();
                        this.table.find("tr").each(function(inx){
                            var oTh = $(this);
                            if(!oTh.hasClass("dgj_trG1")) {
                                oTh.find("td").eq(no)[hideOrShow]();
                            }
                        });
                        if(this.p.canColumeDD) {
                            this.colDDs[colNo][hideOrShow]();
                            var scrollL = this.sbj.getScrollLeft();  //定位拖动条
                            if (scrollL > 0) {
                                this.sbj.setScrollLeft(scrollL + 1);
                                this.sbj.setScrollLeft(scrollL - 1);
                            }
                            else {
                                this.setDDPosition();
                            }
                        }
                        this.setMainHeight();
                        this.setSbjWidth(true);   //设置下滚动条宽
                    }
                    break;
                case 1: {
                        this.tableHead.find("th").eq(no)[hideOrShow]();
                        this.table.find("tr").each(function(inx){
                            $(this).find("td").eq(no)[hideOrShow]();
                        });
                        if(this.p.canColumeDD) {
                            this.colDDs[colNo][hideOrShow]();
                        }
                        this.setWidth();
                        //设置下滚动条位置                                        
                        this.setDDPosition();
                        this.setWidth();
                        this.sbj.left(this.table.width());
                    }
                    break;
                case 2: {
                        var k = colNo - this.p.lockColumn + 1;
                        this.tableHead2.find("th").eq(k)[hideOrShow]();
                        this.table2.find("tr").each(function(inx){
                            $(this).find("td").eq(k)[hideOrShow]();
                        });
                        if(this.p.canColumeDD) {
                            this.colDDs[colNo][0][hideOrShow]();
                            this.colDDs[colNo][1][hideOrShow]();
                        }
                        this.setDDPosition(true);
                        this.setWidth();
                        var scrollL = this.sbj.getScrollLeft();
                        if (scrollL > 0) {
                            this.sbj.setScrollLeft(scrollL + 1);
                            this.sbj.setScrollLeft(scrollL - 1);
                        }
                        else {
                            this.setDDPosition(true);
                        }
                    }
                    break;
                case 3: {
                        var k = colNo - this.p.lockColumnR + 1;
                        this.tableHead3.find("th").eq(k)[hideOrShow]();
                        this.table3.find("tr").each(function(inx){
                            $(this).find("td").eq(k)[hideOrShow]();
                        });
                        if(this.p.canColumeDD) {
                            this.colDDs[colNo][0][hideOrShow]();
                            this.colDDs[colNo][1][hideOrShow]();
                        }
                        this.setWidth();
                        this.sbj.left(this.table.width());
                        //设置下滚动条位置                                        
                        this.setDDPosition();
                        this.setWidth();
                    }
                    break;
            }

            return true;
        },
        //#endregion

        //#region 创建行
        addRow: function (rowData, rowNo, trAfter, hasChk, rowNoG, rowDataG, pageNo) {  //当前行数据,行号,加在某一行后面,是否有复选框
            var thiz = this;
            for (var o in rowData) {   //将空属性附上空字符串
                if (rowData[o] == null) {
                    rowData[o] = "";
                }
            }

            var trB = null, trB2, trB3;
            if (!this.p.groupColumn) {
                trB = $('<tr class="dgj_tr1 dgj_trB_' + rowNo + (!pageNo ? "" : ("_" + pageNo)) + '"></tr>');
            }
            else if (!this.groupObj[rowData[this.p.groupColumn]]) {
                trB = $('<tr class="dgj_tr1"></tr>').css("background-color", this.p.colorParams.trColor3);
                this.groupObj[rowData[this.p.groupColumn]] = "y";
                this.groupArr.push(rowNo);
            }
            if (this.p.lockColumn) {
                trB2 = $('<tr class="dgj_tr2 dgj_trB_' + rowNo + (!pageNo ? "" : ("_" + pageNo)) + '"></tr>');
            }
            if (this.p.lockColumnR) {
                trB3 = $('<tr class="dgj_tr3 dgj_trB_' + rowNo + (!pageNo ? "" : ("_" + pageNo)) + '"></tr>');
            }

            //复选框
            rowData.checked = false;
            if (trB) {
                if (this.p.hasChk) {  //是否有复选框
                    var tdChk = $('<td style="text-align:center;"></td>');
                    var tdDiv = $('<div class="dgj_tdDiv"></div>').css({
                        width: this.p.chkWidth
                    });
                    tdChk.append(tdDiv);

                    (function (d) {
                        if (hasChk) {
                            var chk = $('<input type="checkbox" />');
                            chk.click(function (e, p) {
                                thiz.check(rowNo, rowData, p, chk, pageNo);
                            });
                            thiz.chks.push(chk);
                            tdDiv.append(chk);
                        }
                        trB.append(tdChk);
                    })(rowData);
                }
            }

            //设置隔行变色
            this.setRowColor(rowNo, trB, trB2, trB3);

            //加载行
            if (trB) {
                if (!trAfter) {
                    this.tbody.append(trB);
                }
                else {   //加在某行后面
                    trAfter.after(trB);
                }
            }
            if (this.p.lockColumn) {
                this.tbody2.append(trB2);
            }
            if (this.p.lockColumnR) {
                this.tbody3.append(trB3);
            }

            if (this.p.colorParams.highlight) {  //鼠标移上高亮
                if(!this.p.renderGroupRow){
                    this.setRowHL($(".dgj_trB_" + rowNo + (!pageNo ? "" : ("_" + pageNo))));
                }
                else{
                    this.setRowHL(trB);
                }
            }

            //加载各列
            var thds = this.headContainer.find("div.dgj_tdDiv");
            if (this.p.hasChk) {
                thds = thds.not("div:first");
            }
            var asyncLoadFns = [];
            for (var j = 0, cols = this.p.columns; j < cols.length; j++) {
                var oTrB;
                if (!this.p.lockColumn) {
                    oTrB = trB;
                }
                else {
                    if (j < this.p.lockColumn - 1) {
                        oTrB = trB;
                    }
                    else {
                        if (this.p.lockColumnR) {
                            if (j >= this.p.lockColumnR - 1) {
                                oTrB = trB3;
                            }
                            else {
                                oTrB = trB2;
                            }
                        }
                        else {
                            oTrB = trB2;
                        }
                    }
                }

                if (oTrB) {
                    (function (c) {
                        var tdDiv = $('<div class="dgj_tdDiv"></div>').css({
                            width: thiz.p.defaultW
                        });
                        var td = $('<td></td>').css({
                            height: thiz.p.defaultH
                        });
                        tdDiv.css({   //根据表头列宽确定表体列宽
                            width: parseInt(thds.eq(j).css("width"))
                        });
                        td.append(tdDiv);
                        oTrB.append(td);

                        if (c.align) {   //居中
                            td.css("text-align", c.align);
                        }
                        else {
                            td.css("text-align", thiz.p.defaultAlign);
                        }

                        if(!c.isAsyncLoad) {   //加载数据
                            if (!c.renderFn) {   //是否使用渲染方法
                                tdDiv.append(rowData[c.dataIndex]);
                            }
                            else {
                                tdDiv.append(c.renderFn.call(thiz, rowData[c.dataIndex], rowData, rowNo, rowNoG, rowDataG, pageNo));
                            }
                        }
                        else{
                            tdDiv.append(LMJ.getLoadImg(3));  //显示加载中图标
                            td.css("text-align", "center");   //设置居中
                            tdDiv.attr("alignTmp", c.align);     //保存原本居中方式
                        }

                        if(c.asyncLoadFn){  //异步加载方法
                            asyncLoadFns.push([c.asyncLoadFn, [rowData[c.dataIndex], rowData, rowNo, j]]);
                        }
                    })(cols[j]);
                }
            }

            //执行异步加载方法
            for (var k = 0; k < asyncLoadFns.length; k++) {
                var val = asyncLoadFns[k][1][0];
                var rowData = asyncLoadFns[k][1][1];
                var rowNo = asyncLoadFns[k][1][2];
                var colNo = asyncLoadFns[k][1][3];
                asyncLoadFns[k][0].call(thiz, val, rowData, rowNo, colNo, pageNo);
            }

            //分组创建时加入行集合
            if (rowNoG != null) {
                this.rows[rowNoG].rows.push(trB);
            }

            return trB;
        },
        //#endregion

        //#region 创建分组行
        addRowG: function (rowData, rowNo, trAfter, pageNo) {
            var thiz = this;

            var trG = $('<tr class="dgj_trG1"></tr>').css("background-color", this.p.colorParams.groupRow);
            if (this.p.groupRowH) {
                trG.height(this.p.groupRowH);
            }

            rowData.checked = false;
            if (this.p.hasChkG) {  //是否有复选框
                var chk = $('<input title="" type="checkbox" />');
                chk.click(function (e, p) {
                    thiz.check(rowNo, rowData, p, chk, pageNo);
                });

                var tdChk = $('<td style="text-align:center;"></td>');
                var tdDiv = $('<div class="dgj_tdDiv"></div>').css({
                    width: this.p.chkWidth
                });
                tdChk.append(tdDiv.append(chk));
                trG.append(tdChk);
                thiz.chks.push(chk);
            }

            //加载列数据
            var tdG = $('<td colspan="' + this.p.columns.length + '"></td>');
            trG.append(tdG.append(this.p.renderGroupRow.call(this, rowData, rowNo, pageNo)));

            //加载行
            if (!trAfter) {
                this.tbody.append(trG);
            }
            else {   //加在某行后面
                trAfter.after(trG);
            }

            //加入行集合
            this.rows.push({
                trG: trG,
                rows: []
            });

            return trG;
        },
        //#endregion

        //#region 创建分组行(同时创建组内行)
        addRowGS: function (rowData, rowNo, pageNo) {
            //清空表
            if (this.isDataEmpty()) {
                this.tbody.empty();
            }

            if (rowNo == null) {  //不填行数时加载在最后面
                rowNo = this.data.length;
                this.data.push(rowData);
            }

            //加载分组行
            var trG = this.addRowG(rowData, rowNo, null, pageNo);
            var thiz = this;
            //加载分组内行
            if (this.p.asyncLoadSonRow) {  //异步
                this.p.renderSonRow.call(this, rowNo, trG);
            }
            else {
                if (rowData.sonRows && rowData.sonRows.length > 0) {
                    if(this.p.isLazyLoad) {  //延时加载
                        var j = 0;
                        var fn = function(){
                            if(fn.guidLazy != thiz.guidLazy)   //如果延时加载唯一标记和本次加载不同,退出
                                return;
                            if(j>=rowData.sonRows.length)
                                return;
                            trG = thiz.addRow(rowData.sonRows[j], j, trG, false, rowNo, rowData);
                            j++;
                            thiz.iLazy2++;
                            if(thiz.iLazy2 >= thiz.iLazy1){
                                thiz.setMainHeight();
                            }
                            setTimeout(arguments.callee,0);
                        };
                        fn.guidLazy = this.guidLazy;
                        fn();
                    }
                    else{
                        for (var j = 0; j < rowData.sonRows.length; j++) {
                            trG = this.addRow(rowData.sonRows[j], j, trG, false, rowNo, rowData, pageNo);
                        }
                    }
                }
            }

            this.setTableWidth();
            this.setMainHeight();

            //创建分组行后
            this.fire("afterAddRowGS", { rowData: rowData, rowNo: rowNo, pageNo: pageNo });
        },
        //#endregion

        //#region 删除分组行
        removeRowG: function (rowNo) {
            //删除分组行前
            this.fire("beforeRemoveRowG", { rowData: this.data[rowNo], rowNo: rowNo });

            this.data[rowNo] = null;

            this.rows[rowNo].trG.remove();
            for (var i = 0; i < this.rows[rowNo].rows.length; i++) {
                this.rows[rowNo].rows[i].remove();
            }

            //清空表
            if (this.isDataEmpty()) {
                this.clearData();
            }

            //删除分组行后
            this.fire("afterRemoveRowG", { rowNo: rowNo });
        },
        //#endregion

        //#region 填充单元格数据
        setCellData: function (rowNo, colNo, dataIndex, data, pageNo) {
            if(this.isDataEmpty())
                return;

            var thiz = this;
            var colNoT = colNo;
            if(this.p.hasChk) {  //是否有复选框
                colNoT += 1;
            }

            //更新表数据集
            if(!this.p.isPageCache) {
                this.data[rowNo][dataIndex] = data;
            }
            else{
                var cache = this.getPageCache(pageNo);
                if(cache) {
                    cache[1][rowNo][dataIndex] = data;
                }
                else {
                    this.data[rowNo][dataIndex] = data;
                }
            }

            //加载到表
            var tdDiv, colType = 1;
            if (!this.p.lockColumn) {   //无冻结列
                colType = 1;
            }
            else {   //有冻结列
                if (colNo < this.p.lockColumn - 1) {  //冻结列(左)
                    colType = 1;
                }
                else {
                    if (this.p.lockColumnR) {  //右侧有冻结列
                        if (colNo >= this.p.lockColumnR - 1) {  //冻结列(右)
                            colType = 3;
                        }
                        else {
                            colType = 2;
                        }
                    }
                    else {
                        colType = 2;
                    }
                }
            }

            switch (colType) {
                case 1: {
                        var tbody;
                        if(!this.p.isPageCache) {
                            tbody = this.tbody;
                        }
                        else{
                            var cache = this.getPageCache(pageNo);
                            if(cache) {
                                tbody = cache[2];
                            }
                            else {
                                tbody = this.tbody;
                            }
                        }
                        tdDiv = tbody.find("tr").eq(rowNo).find("div.dgj_tdDiv").eq(colNoT);
                    }
                    break;
                case 2: {
                        var k = colNo - this.p.lockColumn + 1;
                        var tbody2;
                        if(!this.p.isPageCache) {
                            tbody2 = this.tbody2;
                        }
                        else{
                            var cache = this.getPageCache(pageNo);
                            if(cache) {
                                tbody2 = cache[4];
                            }
                            else {
                                tbody2 = this.tbody2;
                            }
                        }
                        tdDiv = tbody2.find("tr").eq(rowNo).find("div.dgj_tdDiv").eq(k);
                    }
                    break;
                case 3: {
                        var k = colNo - this.p.lockColumnR + 1;
                        var tbody3;
                        if(!this.p.isPageCache) {
                            tbody3 = this.tbody3;
                        }
                        else{
                            var cache = this.getPageCache(pageNo);
                            if(cache) {
                                tbody3 = cache[5];
                            }
                            else {
                                tbody3 = this.tbody3;
                            }
                        }
                        tdDiv = tbody3.find("tr").eq(rowNo).find("div.dgj_tdDiv").eq(k); 
                    }
                    break;
            }

            if(tdDiv.attr("alignTmp")) {  //还原居中方式
                tdDiv.css("text-align", tdDiv.attr("alignTmp"));
            }
            tdDiv.html(data);

            //每次填充后设置
            if (this.p.lockColumn) {
                if (this.dgjStoCell) clearTimeout(this.dgjStoCell);
                this.dgjStoCell = setTimeout(function () {
                    thiz.syncRowHeight();  //同步行高
                    //thiz.joinCacheData();  //重新创建缓存数据集
                }, 500);
            }
        },
        //#endregion

        //#region 判断表数据是否为空
        isDataEmpty: function () {
            var b = false;
            for (var i = 0; i < this.data.length; i++) {
                if (this.data[i] != null) {
                    b = true;
                }
            }
            return !b || this.data.length <= 0;
        },
        //#endregion

        //#region 清空数据
        clearData: function (isInit) {
            this.tbody.empty();
            this.data = [];
            this.chks = [];
            this.p.canSort = false;  //禁止排序
            if(this.p.isPageCache){  //清空分页缓存
               this.clearPageCache();
               this.tbody = $('<tbody></tbody>');
               this.table.empty().append(this.tbody);
            }

            var cols = 0;
            if (!this.p.lockColumn) {
                cols = this.p.columns.length;
            }
            else {
                cols = this.p.lockColumn - 1;
            }
            if (this.p.hasChk) {
                cols += 1;
            }

            this.table.width(!FJ.isFF ? this.tableHead.width() : this.tableHead.width() + 1);
            this.tbody.append('<tr><td colspan="' + cols + '" style="text-align:center;height:' + this.p.emptyRowH + 'px;">' + this.p.emptyTxt + '</td></tr>');

            if (this.p.lockColumn) {
                this.tbody2.empty();
                this.table2.width(!FJ.isFF ? this.tableHead2.width() : this.tableHead2.width() + 1);
                this.tbody2.append('<tr><td colspan="' + (this.p.columns.length - this.p.lockColumn + 1) + '" style="text-align:center;height:' + this.p.emptyRowH + 'px;">&nbsp;</td></tr>');

                if (!isInit)
                    this.setWidth();

                if (this.p.lockColumnR) {
                    this.tbody3.empty();
                    this.table3.width(!FJ.isFF ? this.tableHead3.width() : this.tableHead3.width() + 1);
                    this.tbody3.append('<tr><td colspan="' + (this.p.columns.length - this.p.lockColumnR + 1) + '" style="text-align:center;height:' + this.p.emptyRowH + 'px;">&nbsp;</td></tr>');
                }

                //同步表头高度
                this.syncRowHeightH();
            }

            this.setTableWidth();
            this.table.width(!FJ.isFF ? this.tableHead.width() : this.tableHead.width() + 1);

            //设置下滚动条位置
            if (this.p.lockColumn) {
                this.sbj.left(this.table.width());
            }

            this.setMainHeight();
        },
        //#endregion

        //#region 设置列可拖动
        setColumeDD: function () {
            var thiz = this;
            this.colDDs = [];   //拖动对象集合
            this.spjs = [];
            var ths = this.thead.find("th");
            var thds = this.thead.find("div.dgj_tdDiv");
            if (this.p.hasChk) {
                ths = ths.not("th:first");
                thds = thds.not("div:first");
            }

            var ths2, thds2;
            if (this.p.lockColumn) {
                ths2 = this.thead2.find("th");
                thds2 = this.thead2.find("div.dgj_tdDiv");
            }

            var ths3, thds3;
            if (this.p.lockColumnR) {
                ths3 = this.thead3.find("th");
                thds3 = this.thead3.find("div.dgj_tdDiv");
            }

            for (var i = 0, cols = this.p.columns; i < cols.length; i++) {
                var colType = 1;  //列类型
                if (!this.p.lockColumn) {  //无冻结列
                    colType = 0;
                }
                else {   //有冻结列
                    if (i < this.p.lockColumn - 1) {  //冻结列(左)
                        colType = 1;
                    }
                    else {
                        if (this.p.lockColumnR) {  //右侧有冻结列
                            if (i >= this.p.lockColumnR - 1) {  //冻结列(右)
                                colType = 3;
                            }
                            else {
                                colType = 2;
                            }
                        }
                        else {
                            colType = 2;
                        }
                    }
                }

                var colDD = $("<div></div>").css({
                    top: 0,
                    width: 5,
                    height: "100%",
                    //backgroundColor:"red",
                    position: "absolute",
                    zIndex: 1
                }), colDD2;

                switch (colType) {
                    case 0:
                    case 1:
                    case 3:
                        this.colDDs.push(colDD);
                        this.headContainer.append(colDD);
                        if (FJ.isIE6) {   //加入一个空元素解决IE6下拖动条不显示
                            this.headContainer.append("<div></div>");
                        }
                        break;
                    case 2:
                        colDD.addClass("dgj_colDD" + i);
                        colDD2 = colDD.clone().addClass("dgj_colDD" + i);
                        this.lcHeadContainer.append(colDD);
                        this.lcContainer.append(colDD2);
                        this.colDDs.push([colDD, colDD2]);
                        break;
                }

                (function (dd, i) {
                    switch (colType) {
                        case 0: 
                            {
                                var spj = $(dd).SPJ({
                                    oLeft: thds.eq(i),
                                    evts: {
                                        splitEnd: function (e, p) {
                                            thiz.table.find("tr").each(function () {  //设置表体列宽
                                                var trds = $(this).find("div.dgj_tdDiv");
                                                if (thiz.p.hasChk) {
                                                    trds = trds.not("div:first");
                                                }
                                                trds.eq(i).width(p.l);
                                            });

                                            thiz.setMainHeight();
                                            thiz.setSbjWidth(true);   //设置下滚动条宽

                                            var scrollL = thiz.sbj.getScrollLeft();  //定位拖动条
                                            if (scrollL > 0) {
                                                thiz.sbj.setScrollLeft(scrollL + 1);
                                                thiz.sbj.setScrollLeft(scrollL - 1);
                                            }
                                            else {
                                                thiz.setDDPosition();
                                            }
                                        }
                                    }
                                });
                            }
                            break;
                        case 1: 
                            {
                                var spj = $(dd).SPJ({
                                    oLeft: thds.eq(i),
                                    evts: {
                                        splitEnd: function (e, p) {
                                            thiz.table.find("tr").each(function () {  //设置表体列宽
                                                var trds = $(this).find("div.dgj_tdDiv");
                                                if (thiz.p.hasChk) {
                                                    trds = trds.not("div:first");
                                                }
                                                trds.eq(i).width(p.l);
                                            });

                                            thiz.syncRowHeight();
                                            thiz.setMainHeight();
                                            thiz.setWidth();
                                            //设置下滚动条位置                                        
                                            thiz.setDDPosition();
                                            thiz.setWidth();
                                            thiz.sbj.left(thiz.table.width());
                                        }
                                    }
                                });
                            }
                            break;
                        case 2: 
                            {
                                var k = i - thiz.p.lockColumn + 1;
                                var spj = $("div.dgj_colDD" + i).SPJ({
                                    oLeft: thds2.eq(k),
                                    evts: {
                                        splitEnd: function (e, p) {
                                            thiz.table2.find("tr").each(function () {  //设置表体列宽
                                                var trds = $(this).find("div.dgj_tdDiv");
                                                trds.eq(k).width(p.l);
                                            });

                                            thiz.syncRowHeight();
                                            thiz.setMainHeight();
                                            thiz.setDDPosition(true);
                                            thiz.setWidth();
                                            var scrollL = thiz.sbj.getScrollLeft();
                                            if (scrollL > 0) {
                                                thiz.sbj.setScrollLeft(scrollL + 1);
                                                thiz.sbj.setScrollLeft(scrollL - 1);
                                            }
                                            else {
                                                thiz.setDDPosition(true);
                                            }
                                        }
                                    }
                                });
                            }
                            break;
                        case 3: 
                            {
                                var k = i - thiz.p.lockColumnR + 1;
                                var spj = $(dd).SPJ({
                                    oLeft: thds3.eq(k),
                                    evts: {
                                        splitEnd: function (e, p) {
                                            thiz.table3.find("tr").each(function () {  //设置表体列宽
                                                var trds = $(this).find("div.dgj_tdDiv");
                                                trds.eq(k).width(p.l);
                                            });

                                            thiz.syncRowHeight();
                                            thiz.setMainHeight();
                                            thiz.setWidth();
                                            thiz.sbj.left(thiz.table.width());
                                            //设置下滚动条位置                                        
                                            thiz.setDDPosition();
                                            thiz.setWidth();
                                        }
                                    }
                                });
                            }
                            break;
                    }
                    thiz.spjs.push(spj);
                })(colDD, i);
            }
        },
        //#endregion

        //#region 计算拖动条位置
        setDDPosition: function (isOnlySetTable2) {
            var ths = this.thead.find("th");
            if (this.p.hasChk) {
                ths = ths.not("th:first");
            }

            var ths2;
            if (this.p.lockColumn) {
                ths2 = this.thead2.find("th");
            }

            var ths3;
            if (this.p.lockColumnR) {
                ths3 = this.thead3.find("th");
            }

            var spjShiftL = 6;
            if (FJ.isIE7) {
                spjShiftL = 10;
            }
            else if (FJ.isIE8 || FJ.isIE9 || FJ.isFF) {
                spjShiftL = 8;
            }
            else if(FJ.isWebkit) {
                spjShiftL = 12;
            }

            for (var i = 0, cols = this.p.columns; i < cols.length; i++) {
                var colType = 1;  //列类型
                if (!this.p.lockColumn) {  //无冻结列
                    colType = 1;
                }
                else {   //有冻结列
                    if (i < this.p.lockColumn - 1) {  //冻结列(左)
                        colType = 1;
                        if (isOnlySetTable2)
                            continue;
                    }
                    else {
                        if (this.p.lockColumnR) {  //右侧有冻结列
                            if (i >= this.p.lockColumnR - 1) {  //冻结列(右)
                                colType = 3;
                            }
                            else {
                                colType = 2;
                            }
                        }
                        else {
                            colType = 2;
                        }
                    }
                }

                var l = 0;
                switch (colType) {
                    case 1: 
                        {
                            l = ths.eq(i).offset().left;
                            var p = l + (!FJ.isWebkit ? ths.eq(i).width() : ths.eq(i)[0].clientWidth) - spjShiftL;
                            this.colDDs[i].css({
                                left: p
                            });
                        }
                        break;
                    case 2: 
                        {
                            var k = i - this.p.lockColumn + 1;
                            var sl = this.sbj.getScrollLeft();
                            l = ths2.eq(k).offset().left - this.table.width() + sl;
                            var p = l + (!FJ.isWebkit ? ths2.eq(k).width() : ths2.eq(k)[0].clientWidth) - spjShiftL;
                            this.colDDs[i][0].css({
                                left: p
                            });
                            this.colDDs[i][1].css({
                                left: p
                            });
                            this.spjs[i].p.shift.l = sl;
                        }
                        break;
                    case 3: 
                        {
                            var k = i - this.p.lockColumnR + 1;
                            l = ths3.eq(k).offset().left;
                            var p = l + (!FJ.isWebkit ? ths3.eq(k).width() : ths3.eq(k)[0].clientWidth) - spjShiftL;
                            this.colDDs[i].css({
                                left: p
                            });
                        }
                        break;
                }

                //设置拖动条最小宽度
                var minW = 0;
                if (cols[i].minWidth) {
                    minW = cols[i].minWidth;
                }
                else {
                    minW = this.p.defaultMinW;
                }
                this.spjs[i].p.minX = l + minW;

                //设置拖动条最大宽度
                if (cols[i].maxWidth) {
                    this.spjs[i].p.maxX = l + cols[i].maxWidth;
                }
            }

            //表格空时
            if (this.isDataEmpty()) {
                this.table.width(!FJ.isFF ? this.tableHead.width() : this.tableHead.width() + 1);
                if (this.p.lockColumn) {
                    this.table2.width(!FJ.isFF ? this.tableHead2.width() : this.tableHead2.width() + 1);
                }
                if (this.p.lockColumnR) {
                    this.table3.width(!FJ.isFF ? this.tableHead3.width() : this.tableHead3.width() + 1);
                }

                //同步表头高度
                this.syncRowHeightH();
            }
        },
        //#endregion

        //#region 设置横滚动条宽度
        setSbjWidth: function (isSplitEnd) {
            var wTh = this.tableHead.width() + 1;
            var wDo = this.divOut.width();
            var wThH = wTh;
            if (FJ.isIE8 && isSplitEnd) {  //解决ie8下横滚动条不显示
                wDo -= 1;
                wThH += 1;
            }
            this.mainContainer.width(wTh);
            this.mainHeadContainer.width(wThH);
            var wBs = this.bodyContainer[0].scrollWidth;
            this.sbj.width(wDo, wBs);

            if (FJ.isIE8) {
                if (wDo >= wBs) {
                    this.sbj.divOut.hide();
                }
                else {
                    this.sbj.divOut.show();
                }
            }
            else {
                this.sbj.divOut.show();
            }
        },
        //#endregion

        //#region 设置宽度
        setWidth: function (notSetTable2) {
            var thiz = this;

            if (thiz.p.lockColumnR) {
                this.rightContainer.show();
                this.rightHeadContainer.show();
            }

            var fnW = function () {
                var w = 0;
                if (!thiz.p.lockColumnR) {
                    w = thiz.divOut.width() - thiz.table.width() - 18
                }
                else {
                    w = thiz.divOut.width() - thiz.table.width() - 18 - thiz.table3.width();
                    //                    if (FJ.isIE6 || FJ.isIE7) {
                    //                        w -= 17;
                    //                    }
                }
                if (thiz.p.groupColumn && !thiz.isInit) {
                    thiz.isInit = true;
                    w -= 17;
                }

                var wH = w;
                var wB = !FJ.isFF ? w : w - 1;

                //锁定表宽度不足时的处理
                if (!notSetTable2) {
                    var tw2 = thiz.tableHead2.width();
                    if (tw2 > 0 && tw2 < w) {
                        wH = tw2 - 1;
                        wB = tw2 - 1;
                    }
                }

                thiz.lcHeadContainer.show().width(wH);
                thiz.lcContainer.show().width(wB);
            };
            fnW();

            //滚动条位置
            this.sbj.width(this.lcContainer.width() + (!FJ.isFF ? 0 : 2), this.lcContainer[0].scrollWidth);

            //IE6下需要再设置一次宽度
            if (FJ.isIE6) {
                fnW();
            }

            this.fire("afterSetWidth");
        },
        //#endregion

        //#region 同步行高
        syncRowHeight: function () {
            //锁定表
            if (this.p.lockColumn) {
                //同步行高(左>右)
                this.trH2.height(this.trH.height());  //表头
                if (!this.p.groupColumn) {  //表体
                    var oTrs2 = this.tbody2.find("tr");
                    this.tbody.find("tr").each(function (inx) {
                        if (FJ.isFF || FJ.isOpera) {
                            oTrs2.eq(inx).height($(this).height());
                        }
                        else {
                            var td = $(this).find("td").eq(0);
                            var h = td.height() + 1;
                            td.height(h);
                            oTrs2.eq(inx).find("td").eq(0).height(h);
                        }
                    });
                }

                //同步行高(右>左)
                if ((FJ.isIE6 || FJ.isIE7) && this.p.hasChk) {  //表头
                    var oTh = this.trH2.find("th").eq(0);
                    var h = this.trH2.height() - parseInt(oTh.css("padding-top")) - parseInt(oTh.css("padding-bottom")) - parseInt(oTh.css("border-top-width"));
                    if (FJ.isIE7) {
                        h -= parseInt(oTh.css("border-bottom-width"));
                    }
                    this.trH.height(h);
                }
                else {
                    this.trH.height(this.trH2.height());
                }

                //表体
                var oTrs1 = this.tbody.find("tr");
                if (!this.p.groupColumn) {
                    this.tbody2.find("tr").each(function (inx) {
                        if (FJ.isFF || FJ.isOpera) {
                            oTrs1.eq(inx).height($(this).height());
                        }
                        else {
                            var td = $(this).find("td").eq(0);
                            var h = td.height() + 1;
                            td.height(h);
                            oTrs1.eq(inx).find("td").eq(0).height(h);
                        }
                    });
                }
                else {
                    this.groupArr.push(data.length);

                    for (var i = 0, j = 0; i < this.groupArr.length; i++) {
                        if (i == this.groupArr.length - 1) {
                            break;
                        }

                        var h = 0, hs = 0;
                        this.tbody2.find("tr").each(function (inx) {
                            if (inx >= thiz.groupArr[i] && inx < thiz.groupArr[i + 1]) {
                                if ((FJ.isIE6 || FJ.isIE7) && thiz.p.hasChk && hs == 0) {
                                    var oTd = $(this).find("td").eq(0);
                                    hs = parseInt(oTd.css("padding-top")) + parseInt(oTd.css("padding-bottom")) + parseInt(oTd.css("border-top-width"));
                                }
                                h += $(this).height();
                            }
                        });
                        oTrs1.eq(j).height(h - hs);
                        j++;
                    }
                }
            }

            //锁定表(右)
            if (this.p.lockColumnR) {
                this.trH3.css("height", this.trH2.height());
                var oTrs3 = this.tbody3.find("tr");
                this.tbody2.find("tr").each(function (inx) {
                    if (FJ.isFF || FJ.isOpera) {
                        oTrs3.eq(inx).height($(this).height());
                    }
                    else {
                        var td = $(this).find("td").eq(0);
                        var h = td.height() + 1;
                        td.height(h);
                        oTrs3.eq(inx).find("td").eq(0).height(h);
                    }
                });
            }
        },
        //#endregion

        //#region 同步行高(表头)
        syncRowHeightH: function () {
            if (this.p.lockColumn) {
                this.trH2.css("height", this.trH.height());
                if ((FJ.isIE6 || FJ.isIE7) && this.p.hasChk) {
                    var oTh = this.trH2.find("th").eq(0);
                    this.trH.height(this.trH2.height() - parseInt(oTh.css("padding-top")) - parseInt(oTh.css("padding-bottom")) - parseInt(oTh.css("border-top-width")));
                }
                else {
                    this.trH.height(this.trH2.height());
                }

                if (this.p.lockColumnR) {
                    this.trH3.css("height", this.trH2.height());
                    this.tbody2.find("td").attr("colspan", this.p.lockColumnR - this.p.lockColumn + 1);
                }
            }
        },
        //#endregion

        //#region 计算表格宽度
        setTableWidth: function (notSetTable2) {
            var thiz = this;

            if (!this.p.lockColumn) {
                var doW = this.divOut.width();
                var headW = this.tableHead.width();
                var bodyW = this.table.width();
                var w = doW - headW;

                //表头
                var ods = this.tableHead.find("div.dgj_tdDiv");
                var columnP = 0;
                if (this.columnP != null) {
                    if (this.p.hasChk) {
                        columnP = this.columnP + 1;
                    }
                    else {
                        columnP = this.columnP;
                    }
                }
                else {
                    columnP = ods.length - 1;
                }

                var od = ods.eq(columnP);
                var wd = od.width();
                var wh = w + wd - 18;
                if (wh > this.p.defaultW) {
                    od.width(wh);
                }
                else {
                    od.width(this.p.defaultW);
                }

                //表体
                this.table.find("tr.dgj_tr1").each(function () {
                    var ods2 = $(this).find("div.dgj_tdDiv");
                    var od2 = ods2.eq(columnP);
                    var wd2 = od2.width();
                    var wh2 = w + wd - 18;
                    if (wh2 > thiz.p.defaultW) {
                        od2.width(wh);
                    }
                    else {
                        od2.width(thiz.p.defaultW);
                    }
                });

                //设置横滚动条宽度
                this.setSbjWidth();

                //计算拖动条位置
                if (this.p.canColumeDD) {
                    this.setDDPosition();
                }
            }
            else {
                //动态设置锁定列表宽度
                this.setWidth(notSetTable2);

                //计算拖动条位置
                if (this.p.canColumeDD) {
                    this.setDDPosition();
                }
            }

            //表格空时的设定
            if (this.isDataEmpty()) {
                this.table.width(!FJ.isFF ? this.tableHead.width() : this.tableHead.width() + 1);
            }
            else {
                this.table.css("width", "auto");
                if(FJ.isIE7) {
                    //alert(this.tableHead.width());
                    //this.table.width(1000);
                }
                if (this.p.lockColumn) {
                    this.table2.css("width", "auto");
                }
                if (this.p.lockColumnR) {
                    this.table3.css("width", "auto");
                }
            }
        },
        //#endregion

        //#region 计算主体高度
        setMainHeight: function () {
            var parentH = this.divOut.parent().height();
            var headH = this.tableHead.height();
            var bodyH = this.table.height();
            if (this.p.lockColumn) {
                var bodyH2 = this.table2.height();
                var bodyH3 = 0;
                if (this.p.lockColumnR) {
                    bodyH3 = this.table3.height();
                }
                var maxH = Math.max(bodyH, bodyH2, bodyH3);
                bodyH = maxH;
            }
            var sbjH = this.sbj.p.heightF;
            var allH = headH + bodyH + sbjH;

            if (allH >= parentH) {
                this.bodyContainer.height(parentH - headH - this.sbj.p.heightF);
            }
            else {
                this.bodyContainer.height(bodyH + 1);
            }
        },
        //#endregion

        //#region 设置行高亮
        setRowHL: function (tr) {
            var thiz = this;
            tr.unbind("mouseenter").unbind("mouseleave").hover(function () {
                if (jQuery.fx.step.backgroundColor) {
                    tr.animate({ backgroundColor: thiz.p.colorParams.highlight }, 50, function () {
                        tr.css("background-color", thiz.p.colorParams.highlight);
                    });
                }
                else {
                    tr.css("background-color", thiz.p.colorParams.highlight);
                }
            }, (function () {
                if (jQuery.fx.step.backgroundColor) {
                    tr.stop().animate({ backgroundColor: tr.attr("bgcolor") }, 200, function () {
                        tr.css("background-color", tr.attr("bgcolor"));
                    });
                }
                else {
                    tr.css("background-color", tr.attr("bgcolor"));
                }
            }));
        },
        //#endregion

        //#region 设置隔行变色
        setRowColor: function (rowNo, trB, trB2, trB3) {
            if (rowNo % 2 > 0) {  //设置隔行变色
                if (!this.p.groupColumn)
                    trB.css("background-color", this.p.colorParams.trColor2).attr("bgcolor", this.p.colorParams.trColor2);
                if (this.p.lockColumn)
                    trB2.css("background-color", this.p.colorParams.trColor2).attr("bgcolor", this.p.colorParams.trColor2);
                if (this.p.lockColumnR)
                    trB3.css("background-color", this.p.colorParams.trColor2).attr("bgcolor", this.p.colorParams.trColor2);
            }
            else {
                if (!this.p.groupColumn)
                    trB.css("background-color", this.p.colorParams.trColor1).attr("bgcolor", this.p.colorParams.trColor1);
                if (this.p.lockColumn)
                    trB2.css("background-color", this.p.colorParams.trColor1).attr("bgcolor", this.p.colorParams.trColor1);
                if (this.p.lockColumnR)
                    trB3.css("background-color", this.p.colorParams.trColor1).attr("bgcolor", this.p.colorParams.trColor1);
            }
        },
        //#endregion

        //#region 选中
        check: function (rowNo, rowData, isAllCheck, chk, pageNo) {
            var rowData = rowData, chk = chk, isClick = true;
            if(!rowData){
                rowData = this.data[rowNo];
            }
            if(!chk){
                isClick = false;
                chk = this.chks[rowNo];
            }

            if(isClick && !isAllCheck && chk.attr("checked") == rowData.checked)  //防止鼠标双击时执行多次选中事件
                return;

            if (rowData) {
                if (rowData.checked) {
                    if(!isClick)
                        chk.attr("checked", false);
                    rowData.checked = false;
                }
                else {
                    if (this.p.isCheckOne) {  //是否单选
                        if(!this.p.isPageCache) {
                            for (var i = 0; i < this.chks.length; i++) {
                                if(this.chks[i] != chk) {
                                    this.chks[i].attr("checked", false);
                                }
                            }
                            for (var i = 0; i < this.data.length; i++) {
                                this.data[i].checked = false;
                            }
                        }
                        else {  //使用分页缓存时计算所有缓存中数据 
                            for (var h = 0; h < this.pageCache.length; h++) {
                                var chks = this.pageCache[h][3];
                                var data = this.pageCache[h][1];
                                for (var i = 0; i < chks.length; i++) {
                                    if(chks[i] != chk) {
                                        chks[i].attr("checked", false);
                                    }
                                }
                                for (var i = 0; i < data.length; i++) {
                                    data[i].checked = false;
                                }
                            }
                        }
                    }
                    if(!isClick)
                        chk.attr("checked", true);
                    rowData.checked = true;
                }

                //选中事件
                if (!isAllCheck) {
                    this.fire("checkedOne", { rowData: rowData, rowNo: rowNo, checked: rowData.checked, pageNo: pageNo });
                }
            }
        },
        //#endregion

        //#region 全选
        fnChkAll: function (chk) {
            for (var i = 0; i < this.chks.length; i++) {
                if (chk.checked ? !this.chks[i].attr("checked") : this.chks[i].attr("checked")) {
                    //this.check(i, null, true);
                    this.chks[i].trigger("click", true);
                }
            }
        },
        //#endregion

        //#region 排序
        sort: function (isAsc, colNo, colName) {
            var thiz = this;
            if(this.isDataEmpty())
                return;

            //先将数据集合每行标记一个原始索引,供表排序
            for(var i = 0;i < this.data.length;i++){
                this.data[i].sortNo = i;
            }

            //判断排序列数据类型
            var fnCompare = FJ.Sort.compareStringEN;
            try{
                for(var i = 0;i < this.data.length;i++){
                    var colVal = this.data[i][colName];
                    if(!isNaN(colVal)){  //数字
                        fnCompare = FJ.Sort.compareNumber;
                        break;
                    }
                    else if(FJ.Date.parse(colVal) != "Invalid Date" && FJ.Date.parse(colVal) != "NaN"){  //日期
                        fnCompare = FJ.Sort.compareDate;
                        break;
                    }
                    else if(FJ.RX.chFirst(colVal)){  //中文
                        fnCompare = FJ.Sort.compareStringCH;
                        break;
                    }
                    else{  //其他字符
                        fnCompare = FJ.Sort.compareStringEN;
                    }
                }
            }
            catch(e){
                fnCompare = FJ.Sort.compareStringEN;
            }

            //排序数据集合
            this.data = this.data.sort(function(a, b){
                return fnCompare(a[colName], b[colName], isAsc);
            });

            //排序复选框集合
            if(this.p.hasChk){
                var chksTmp = [];
                for(var i = 0;i < this.data.length;i++){
                    var chk = this.chks[this.data[i].sortNo];
                    (function(i, chk){   //因为行号要排序所以重新绑定事件
                        chk.unbind("click").click(function (e, p) {
                            thiz.check(i, thiz.data[i], p, chk, thiz.pageNo);
                        });
                    })(i, chk);
                    chksTmp.push(chk);
                }
                this.chks.length = 0;
                for(var i = 0;i < chksTmp.length;i++){  //填充至原数组中
                    this.chks.push(chksTmp[i]);
                }
            }

            //排序表
            var trs = this.tbody.find("tr"), trs2, trs3;
            if (this.p.lockColumn) {
                trs2 = this.tbody2.find("tr");
            }
            if (this.p.lockColumnR) {
                trs3 = this.tbody3.find("tr");
            }
            for(var i = 0;i < this.data.length;i++){
                this.tbody.append(trs.eq(this.data[i].sortNo));
                if (this.p.lockColumn) {
                    this.tbody2.append(trs2.eq(this.data[i].sortNo));
                }
                if (this.p.lockColumnR) {
                    this.tbody3.append(trs3.eq(this.data[i].sortNo));
                }
            }

            //重新设置隔行变色
            var trsC2, trsC3;
            if (this.p.lockColumn) {
                trsC2 = this.tbody2.find("tr");
            }
            if (this.p.lockColumnR) {
                trsC3 = this.tbody3.find("tr");
            }
            this.tbody.find("tr").each(function(inx){
                var oTr = $(this);
                thiz.setRowColor(inx, oTr, trsC2 ? trsC2.eq(inx) : null, trsC3 ? trsC3.eq(inx) : null);
                if(!thiz.p.renderGroupRow && thiz.p.lockColumn){
                    thiz.setRowHL($(".dgj_trB_" + inx + (!thiz.pageNo ? "" : ("_" + thiz.pageNo))));
                }
                else{
                    thiz.setRowHL(oTr);
                }
            });
        },
        //#endregion

        //#region 获取一维数据集合
        getRowsData: function (d) {
            var data;
            if(!d){
                data = this.data;
            }
            else{
                data = d;
            }

            if (data && data.length > 0 && this.p.renderGroupRow) {
                var dataR = [];
                var fn = function(data) {
                    for (var i = 0; i < data.length; i++) {
                        if (data[i] != null) {
                            for (var j = 0; j < data[i].sonRows.length; j++) {
                                var obj = data[i].sonRows[j];
                                for (var o in data[i]) {
                                    if (o != "sonRows") {
                                        obj[o] = data[i][o];
                                    }
                                }
                                dataR.push(obj);
                            }
                        }
                    }
                };

                if(!this.p.isPageCache) {
                    fn(data);
                }
                else{
                    for (var h = 0; h < this.pageCache.length; h++) {
                        fn(this.pageCache[h][1]);
                    }
                }

                return dataR;
            }
            else {
                return null;
            }
        }
        //#endregion
    });

    //#region 绑定到jquery
    $.fn.extend({
        DatagridJ: function (settings) {
            return new FJ.DatagridJ(this, settings);
        },
        DGJ: function (settings) {
            return $(this).DatagridJ(settings);
        }
    });
    //#endregion

    //#region 加载css
    $("head").append('<link id="DatagridJStyle" href="' + FJ.cssPath + 'flareJ.Datagrid.css" rel="stylesheet" type="text/css" />');
    //#endregion

})(jQuery);